package com.my.control;

import java.util.List;

import com.my.bean.Address;
import com.my.util.PageBean;

public class AddressAction extends BaseAction {

	private String oldAddressName;

	public String getOldAddressName() {
		return oldAddressName;
	}

	public void setOldAddressName(String oldAddressName) {
		this.oldAddressName = oldAddressName;
	}

	public String listAddress() throws Exception {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		int totalRecords = 0;
		List<Address> list;
		list = addressbo.getAllAddressByPage(pageNo, 15);
		totalRecords = addressbo.getTotalAddressRecords().intValue();
		PageBean<Address> pagebean = this.setPageBean(list, totalRecords);
		pagebean.setPageAction("listAddress_add");
		return "chaxun";
	}

	public String searchAddress() throws Exception {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		int totalRecords = 0;
		List<Address> list;
		list = addressbo.searchAddressByPage(pageNo, 15, address);
		totalRecords = addressbo.searchTotalAddressRecords(address).intValue();
		PageBean<Address> pagebean = this.setPageBean(list, totalRecords);
		pagebean.setPageAction("searchAddress_add");
		return "chaxun";
	}

	public String listAddressPrepare() throws Exception {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		int totalRecords = 0;
		List<Address> list;
		list = addressbo.getAllAddressByPage(pageNo, 15);
		totalRecords = addressbo.getTotalAddressRecords().intValue();
		PageBean<Address> pagebean = this.setPageBean(list, totalRecords);
		pagebean.setPageAction("listAddressPrepare_add");
		return "listaddress";
	}

	public String searchAddressList() throws Exception {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		int totalRecords = 0;
		List<Address> list;
		list = addressbo.searchAddressByPage(pageNo, 15, address);
		totalRecords = addressbo.searchTotalAddressRecords(address).intValue();
		PageBean<Address> pagebean = this.setPageBean(list, totalRecords);
		pagebean.setPageAction("searchAddressList_add");
		return "listaddress";
	}

	public String editAddressPrepare() throws Exception {
		address = addressbo.getAddressById(Address.class, address.getId());
		request.put("address", address);
		return "editaddress";
	}

	public String addAddress() throws Exception {
		String addressName = address.getName();
		if (addressName.isEmpty()) {
			request.put("message", "地址不能为空!");
		} else if (addressbo.isExist(addressName)) {
			request.put("message", "该地址已经存在!");
		} else if (addressbo.addAddress(address)) {
			request.put("message",
					"<script laguage='JavaScript'> alert('添加成功!') </script>");
			return this.listAddressPrepare();
		} else {
			request.put("message", "添加失败!");
		}
		return "addaddress";
	}

	public String editAddress() throws Exception {
		String addressName = address.getName();
		if (addressName.isEmpty()) {
			request.put("message", "地址不能为空!");
			return this.editAddressPrepare();
		} else if (!addressName.equals(oldAddressName)) {
			if (addressbo.isExist(addressName)) {
				request.put("message", "地址名已经存在!");
				return this.editAddressPrepare();
			}
		}
		if (addressbo.editAddress(address)) {
			request.put("message",
					"<script laguage='JavaScript'> alert('修改成功!') </script>");
			return this.listAddressPrepare();
		} else {
			request.put("message", "修改失败!");
		}
		return this.listAddressPrepare();
	}

	public String removeAddress() throws Exception {
		addressbo.removeAddress(address.getId());
		return this.listAddressPrepare();
	}
}
